var vue = new Vue({
    el: "#main",
    data() {
        return {
            active: 1,
            userInfo: {},
            num: 0, //隐患未完成提示条数
            showSick: false, //是否显示关爱模块
            showPlace: false, //是否显示场所模块
            userPlaceRole: '', //用户场所角色 ，administrator管理员、clerk普通职员
            userSickRole: '', //用户关爱角色 ，administrator管理员、clerk普通职员
            week: '', //关爱的第几周
            year: '', //年份
            care_rate: '', //走访率
            yearArr: [], //年份数组
            care_flage: false,
            num: '',
        }
    },
    created() {
        this.getTime()
        this.userInfo1()
    },
    mounted() {
        // this.getNumber()
        window.addEventListener("visibilitychange", () => {
            if (document.hidden) {

            } else {
                this.userInfo1()
            }
        })
    },
    methods: {
        // 获取时间
        getTime() {
            let time = new Date()
            // this.year = time.getFullYear()
            // let firstDayOfYear = new Date(time.getFullYear(), 0, 1)
            // const dayOfWeek = firstDayOfYear.getDay()
            // let spendDay = 1;
            // if (dayOfWeek != 0) {
            //     spendDay = 7 - dayOfWeek + 1;
            // }
            // firstDayOfYear = new Date(time.getFullYear(), 0, spendDay);
            // const d = Math.ceil((time.valueOf() - firstDayOfYear.valueOf()) / 86400000);
            // 获取当前周数
            // this.week = Math.ceil(d / 7);

            // let currentMonth = time.getMonth() + 1;
            // if (currentMonth >= 1 && currentMonth <= 3) {
            //     this.rateIndex = 0;
            // } else if (currentMonth >= 4 && currentMonth <= 6) {
            //     this.rateIndex = 1;
            // } else if (currentMonth >= 7 && currentMonth <= 9) {
            //     this.rateIndex = 2;
            // } else {
            //     this.rateIndex = 3;
            // }
            let _year = time.getFullYear() - 2023
            for (let i = 0; i <= _year; i++) {
                this.yearArr.push(time.getFullYear() - i)
            }
        },
        // 获取个人信息
        userInfo1() {
            ajax({
                url: formal + '/person/info',
                type: 'GET',
                success: (res) => {
                    console.log(3333)
                    localStorage.setItem("userInfo", JSON.stringify(res.data))
                    this.userInfo = res.data
                    this.year = res.data.year * 1
                    this.week = res.data.week * 1
                    this.care_rate = res.data.care_rate

                    // 用于授权的字段
                    let is_community = res.data.is_community
                    let is_grid = res.data.is_grid
                    let is_protector = res.data.is_protector
                    let has_place_check = res.data.auth.has_place_check
                    let has_sick_man = res.data.auth.has_sick_man

                    // 超级管理员
                    if (!is_community && !is_grid && !is_protector && has_place_check && has_sick_man) {
                        this.showSick = true
                        this.showPlace = true
                        this.userPlaceRole = 'administrator'
                        this.userSickRole = 'administrator'
                    }
                    // 单独的关爱社区管理员 授权
                    else if (is_community) {
                        this.showSick = has_sick_man
                        this.showPlace = has_place_check
                        this.userPlaceRole = 'clerk'
                        this.userSickRole = 'administrator'
                    } else {
                        // 单独的关爱责任人 或者网格员 授权
                        if (is_protector || is_grid) {
                            this.showSick = has_sick_man
                            this.showPlace = has_place_check
                            this.userPlaceRole = 'clerk'
                            this.userSickRole = 'clerk'
                        }
                    }
                    // // 单独是关爱的超级管理员
                    // if (!is_community && !is_grid && !is_protector && !has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = false
                    //     this.userPlaceRole = 'clerk'
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 关爱超级管理员+网格员
                    // if (!is_community && is_grid && !is_protector && has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = true
                    //     this.userPlaceRole = 'clerk'
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 单独的关爱社区管理员
                    // if (is_community && !is_grid && !is_protector && !has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = false
                    //     this.userPlaceRole = ''
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 单独的关爱社区管理员 + 网格员
                    // if (is_community && is_grid && !is_protector && has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = true
                    //     this.userPlaceRole = 'clerk'
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 单独的关爱社区管理员 + 关爱责任人
                    // if (is_community && !is_grid && is_protector && has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = false
                    //     this.userPlaceRole = ''
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 单独的关爱社区管理员 + 关爱责任人
                    // if (is_community && !is_grid && is_protector && !has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = false
                    //     this.userPlaceRole = ''
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 单独的关爱社区管理员 + 网格员+单独的社区关爱责任人,两种都是
                    // if (is_community && is_grid && is_protector && !has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = true
                    //     this.userPlaceRole = 'clerk'
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 单独的关爱社区管理员 + 网格员+单独的社区关爱责任人
                    // if (is_community && is_grid && is_protector && has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = true
                    //     this.userPlaceRole = 'clerk'
                    //     this.userSickRole = 'administrator'
                    // }
                    // // 单独的社区关爱责任人
                    // if (!is_community && !is_grid && is_protector && !has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = false
                    //     this.userPlaceRole = ''
                    //     this.userSickRole = 'clerk'
                    // }
                    // // 单独的社区关爱责任人+网格员 ，
                    // if (!is_community && is_grid && is_protector && has_place_check && has_sick_man) {
                    //     this.showSick = true
                    //     this.showPlace = true
                    //     this.userPlaceRole = 'clerk'
                    //     this.userSickRole = 'clerk'
                    // }

                    // // 单独+网格员
                    // if (!is_community && is_grid && !is_protector && has_place_check && !has_sick_man) {
                    //     this.showSick = false
                    //     this.showPlace = true
                    //     this.userPlaceRole = 'clerk'
                    //     this.userSickRole = ''
                    // }
                    // 根据权限判断


                }
            })
        },
        // 获取关爱走访率
        getCare() {
            ajax({
                url: formal + `/person/info?year=${this.year}&week=${this.week}`,
                type: 'GET',
                success: (res) => {
                    this.care_rate = res.data.care_rate
                    this.care_flage = false
                },
                error: () => {
                    this.care_flage = false
                }
            })
        },
        // 获取隐患未完成提示条数
        getNumber() {
            ajax({
                url: formal + '/place-check/rectify-tip',
                type: 'GET',
                success: (res) => {
                    this.num = res.count
                }
            })
        },
        // 改变关爱走访率年
        changeYear() {
            this.getCare()
        },
        // 改变关爱走访率周
        changeRate(val) {
            if (this.care_flage) {
                return
            }
            if (val == -1) {
                if (this.week == 1) {
                    vant.Toast('已经是本年的第一周');
                    return
                }
            } else {
                if (this.week == 52) {
                    vant.Toast('已经是本年的最后一周');
                    return
                }
            }
            this.care_flage = true
            this.week = this.week + val
            this.getCare()
        },
        // 退出登录
        exitBtb() {
            localStorage.removeItem("token")
            localStorage.removeItem("loginTime")
            window.location.href = "login.html"
        },
        // 跳转到个人新增场所列表
        goPlace() {
            window.location.href = "placeManagement.html"
        },
        // 根据角色信息跳转关爱的对应页面
        goCare() {
            if (this.userSickRole == 'administrator') {
                // 管理员
                window.location.href = "../seekList.html?year=" + this.year + '&week=' + this.week
            } else {
                // 非管理员
                window.location.href = "../seekPeopleList.html?id=" + this.userInfo.protector_grid_id + '&user_id=' + this.userInfo.user_id
            }
        },
        // 根据角色信息跳转场所隐患的对应页面
        goPlaceInspectList() {
            if (this.userPlaceRole == 'administrator') {
                // 管理员
                window.location.href = "../placeInspectList.html?is_grid=0"
            } else {
                // 非管理员
                window.location.href = "../placeInspectList.html?is_grid=1&grid_id=" + this.userInfo.grid_id
            }
        }
    }
})